package com.wptk.filemanager.ad.utils

import android.util.Log
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings


/**
 * @author zhangwei on 2022/6/11.
 * firebase 远程配置
 */
object RemoteConfig {
    private var remoteConfig: FirebaseRemoteConfig? = null

    init {
        if (remoteConfig == null) {
            remoteConfig = Firebase.remoteConfig
            val configSettings = remoteConfigSettings {
                minimumFetchIntervalInSeconds = 12 * 60
            }
            remoteConfig?.setConfigSettingsAsync(configSettings)
            remoteConfig?.fetchAndActivate()
        }
    }

    fun init() {
//        val dynamicData = AssetsUtils.getDataFromAsset(BaseApp.getApplication(), "dynamic_config.json")
        val map = hashMapOf<String, Any>()
        map["dynamicConfig"] = "1345798"
        remoteConfig?.setDefaultsAsync(map)
//        Logx.d("serverConfig defaults:${LogJsonUtil.formatJson(data)}")
    }

    fun getDynamicConfigJson(): String {
        Log.d("RemoteConfig","${remoteConfig?.getString("Dynamic_config")}")
        return "${remoteConfig?.getString("Dynamic_config")}"
    }

//    fun displayWelcomeMessage() {
//        Logx.d("${remoteConfig?.getString("serverConfig")}")
//    }

//    fun getDynamicConfig(): DynamicConfigBean {
//        return try {
//            Logx.d("${remoteConfig?.getString("dynamicConfig")}")
//            GsonUtils.fromJson(remoteConfig?.getString("dynamicConfig"), DynamicConfigBean::class.java)
//        } catch (e: Throwable) {
//            DynamicConfigBean()
//        }
//    }
}